﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace com.dbo
{
    public class Login
    {
        com.net.Login data;
        public Login()
        {
            data = new com.net.Login();
        }
        public Login(com.net.Login data)
        {
            this.data = data;
        }
        public com.net.Login Data
        {
            get { return data; }
            set { data = value; }
        }

        public bool FindUser()
        {
           object c=dbo.DB.getScalar("select username from adc_login where username='" + data.Username + "'");
           if (c == null)
               return false;
           if (c.ToString().Equals(data.Username))
               return true;
           else
               return false; 
        }

        public bool Fetch()
        {
           
            DataTable dt = com.dbo.DB.getTable("select * from adc_login where username='" + data.Username + "'");
            if (dt.Rows.Count == 0)
                return false;

            data.Password = dt.Rows[0][1].ToString();
            data.UserRole = dt.Rows[0][2].ToString();
            if (dt.Rows[0][3] != null)
                data.LastLogin = (DateTime)dt.Rows[0][3];
            data.LoginCreated = (DateTime)dt.Rows[0][4];
            data.Status = dt.Rows[0][5].ToString();
            return true;
        }
        public bool Add()
        {
            
            SqlCommand cmd = com.dbo.DB.getCmdSp("adduser");
            cmd.Parameters.AddWithValue("username", data.Username);
            cmd.Parameters.AddWithValue("password", data.Password);
            cmd.Parameters.AddWithValue("userrole", data.UserRole);
            cmd.Connection.Open();
           int i=cmd.ExecuteNonQuery();
            cmd.Connection.Close();
            return i == 1;
        }
        public bool IsAuthenticate()
        {
            DataTable dt = com.dbo.DB.getTable("select * from adc_login where username='" + data.Username + "' and password='" + data.Password + "'");
            if (dt.Rows.Count == 0)
                return false;

            
            data.Password = dt.Rows[0][1].ToString();
            data.UserRole = dt.Rows[0][2].ToString();
            if (dt.Rows[0][3] != null)
                data.LastLogin = (DateTime)dt.Rows[0][3];
            data.LoginCreated = (DateTime)dt.Rows[0][4];
            data.Status = dt.Rows[0][5].ToString();
            return true;
            
        }
        public bool ChangePassword(string newpass)
        {
            SqlCommand cmd = com.dbo.DB.getCmdSp("changepass");
            cmd.Connection.Open();
            cmd.Parameters.AddWithValue("username", data.Username);
            cmd.Parameters.AddWithValue("password", data.Password);
            cmd.Parameters.AddWithValue("oldpassword", newpass);

            int i = cmd.ExecuteNonQuery();
            cmd.Connection.Close();
            return i == 1;
        }
        public void Delete()
        {
            SqlCommand cmd = com.dbo.DB.getCmdSp("deluser");
            cmd.Connection.Open();
            cmd.Parameters.AddWithValue("username", data.Username);
            
            int i = cmd.ExecuteNonQuery();
            cmd.Connection.Close();
            return i == 1;
        }

    }
}